// pages/recentActivity/recentActivityDetail.js
import {
  my_request,
  my_showToast_error
} from '../../utils/my_utils'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    short_summary: '',
    long_summary: '',
    id: '',
    meeting_time: '',
    dataLength: '',
    meeting_name: '',
    address: '',
    cover: '',
    user_id: '',
    detailInfo: false,
    attendUserInfo: undefined,
    showUserInfo: false,
    contactInfo: undefined
  },
  toggleHideInfo() {
    this.setData({
      showUserInfo: !this.data.showUserInfo
    })
  },
  nav: function (e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      wx.navigateTo({
        url: url
      })
    }
  },
  save_props: function (options) {
    var query_data = { ...options
    };
    query_data.back = "cache"
    this.setData({
      query_data: query_data
    })
  },
  add_props: function (key, value) {
    var query_data = { ...this.data.query_data
    };
    query_data[key] = value;
    this.setData({
      query_data: query_data
    });
  },
  load_process: function (conference_id) {
    var that = this;
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/conference-process',
      method: "GET",
      data: {
        meeting_id: this.data.id
      },
      success: function (res) {
        var arr = res.data.data.process;
        arr = arr.map(function (value) {
          value.content = value.content.replace(/(\r\n)|(\n)/g, '$').split('$');
          // console.log(value.content);
          return value;
        })
        that.setData({
          detailInfo: arr,
        })
      },
      fail: function (res) {
        my_showToast_error('加载失败，请稍后重试', 2000);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.save_props(options)
    var userInfo = wx.getStorageSync('userInfo');
    var self = this;
    var id = options.id;
    var preview = options.edit_id;
    if (preview) {
      self.setData({
        preview: true
      })
      id = preview
    }
    self.setData({
      id: id,
      user_id: userInfo.user_id
    })
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/conference-detail',
      method: "GET",
      data: {
        meeting_id: id
      },
      success: function (res) {
        var infoArr = res.data.data.conference;
        self.load_process(self.data.id);
        self.setData({
          short_summary: infoArr.short_summary,
          long_summary: infoArr.long_summary,
          meeting_time: infoArr.meeting_time,
          meeting_name: infoArr.meeting_name,
          address: infoArr.meeting_address,
          cover: infoArr.cover,
          is_checked: infoArr.is_checked,
          loaded: true,
          meeting_state: infoArr.state
        })
      },
      fail: function (res) {
        my_showToast_error("会议加载失败，请一会刷新", 2000)
      }
    })
    
    my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/my-attend-status',
      method: "GET",
      data: {
        meeting: id
      },
      success: function (resp) {
        let attendInfo = resp.data.data.attend
        
        self.mapUserInfo(attendInfo)

        self.setData({
          attendInfo
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  send_to_check: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '此报告一旦提交审核将不允许任何操作（包括删除在内）',
      success: function (res) {
        if (res.confirm) {
          var data = {
            meeting: +that.data.query_data.edit_id,
            publisher: +that.data.query_data.publisher,
          }
          my_request({
            url: "https://sichuangyi.demopp.in/api-conference-alpha/submit-for-check",
            method: 'POST',
            data: data,
            success: function () {
              my_showToast_error("提交成功", 2000)
              wx.setStorageSync("from-send-to-check", true)
              wx.navigateBack({
                delta: 2
              })
            },
            fail: function () {
              my_showToast_error("提交失败", 2000)
            }
          })

        } else if (res.cancel) {
          //console.log('用户点击取消')
          //wx.navigateBack()
        }
      }
    })
  },
  mapUserInfo(user) {
    if (!user || typeof (user) !== 'object') {
      return
    }

    let userType = [{
      title: '观众',
      type: 2,
    }, {
      title: '嘉宾',
      type: 3,
    }, {
      title: '媒体',
      type: 4,
    }, {
      title: '工作人员',
      type: 5,
    }]

    // get current user type show text
    let curUserType = userType.reduce((acc, cur) => {
      if (cur.type === user.user_type) {
        acc = cur.title
      }
      return acc
    }, null)
    
    user.user_type = curUserType

    // show user info line label
    let showText = {
      "name": "姓名",
      "status": user.status === 1 ? '已签到' : '已报名',
      "created_at": '报名日期',
      // "user_id": "hide",
      // "meeting_id": "hide",
      "updated_at": "报名日期",
      "company": "公司名",
      "id_card": "身份证号",
      "user_type": "参会身份",
      "sex": "性别",
      "phone": "电话号码",
      "position": "职位",
      // "id": "hide",
      "organization": "媒体单位",
      "email": "邮箱"
    }

    let showSequence = [
      'name',
      'user_type',
      'sex',
      'company',
      'organization',
      'id_card',
      'position',
      'phone',
      'email',
      'updated_at'
    ]

    let contactText = {
      'contact_name': '联系人',
      'contact_org': '主办方',
      'contact_phone': '联系方式'
    }

    let contactSequence = [
      'contact_org',
      'contact_name',
      'contact_phone'
    ]

    let showUserInfo = []
    let contactInfo = []

    for (let key of showSequence) {
      if (showText[key] && user[key]) {
        showUserInfo.push({
          label: showText[key],
          value: user[key]
        })
      }
    }

    for (let key of contactSequence) {
      if (contactText[key] && user[key]) {
        contactInfo.push({
          label: contactText[key],
          value: user[key]
        })
      }
    }

    this.setData({
      attendUserInfo: showUserInfo,
      contactInfo: contactInfo.length > 0 ? contactInfo : undefined,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: '来自企创宝',
      path: '/pages/ActivityDetail/index?id=' + this.data.id,
      success: function (res) {
        console.log("转发成功");
      },
      fail: function (res) {
        console.log("转发失败");
      }
    }
  }
})